package com.bestcem.xm.om.dao.impl;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.om.dao.OmRoleDao;
import com.bestcem.xm.om.dao.mapper.OmRoleMapper;
import com.bestcem.xm.om.entity.OmRole;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author zq.ding <zq.ding@idiaoyan.com>
 * @version v1.0
 * @date 2021/12/24 17:08
 */
@Repository
public class OmRoleDaoImpl implements OmRoleDao {

    @Resource
    private OmRoleMapper omRoleMapper;

    @Override
    public List<OmRole> listRole(Integer page, Integer size) {
        if (Objects.nonNull(page) && Objects.nonNull(size)) {
            return omRoleMapper.listRole((page - 1) * size, size);
        }
        return omRoleMapper.listRole(null, null);
    }

    @Override
    public OmRole saveRole(OmRole omRole) {
        LocalDateTime now = LocalDateTime.now();
        omRole.setUpdateTime(now);
        String id = omRole.getId();
        if (Objects.nonNull(id)) {
            omRoleMapper.updateRoleById(omRole);
            return omRole;
        } else {
            omRole.setId(ObjectId.next());
            omRole.setCreateTime(now);
            omRoleMapper.saveRole(omRole);
            return omRole;
        }
    }

    @Override
    public OmRole findById(String id) {
        return omRoleMapper.findById(id);
    }

    @Override
    public List<OmRole> findByIds(List<String> ids) {
        return omRoleMapper.findByIds(ids);
    }

    @Override
    public void deleteById(String id) {
        omRoleMapper.deleteById(id);
    }

    @Override
    public OmRole findByTitle(String title) {
        return omRoleMapper.findByTitle(title);
    }

    @Override
    public List<OmRole> listByTitle(String titlePattern) {
        return omRoleMapper.listByTitle(titlePattern);
    }

    @Override
    public int count() {
        return omRoleMapper.count();
    }

    @Override
    public OmRole findSuperRoleId() {
        return omRoleMapper.findSuperRoleId();
    }

    @Override
    public List<OmRole> getSaleLeader() {
        return omRoleMapper.getSaleLeader();
    }

    @Override
    public List<OmRole> findByCondition(OmRole omRole) {
        return omRoleMapper.findByCondition(omRole);
    }

    @Override
    public List<OmRole> listByIds(List<String> roleIds) {
        return omRoleMapper.findByIds(roleIds);
    }

    @Override
    public List<OmRole> listAll() {
        return omRoleMapper.listRole(null, null);
    }
}
